﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity.Framework;
using Business.Operations;

namespace HPDMAS.UserControls
{
    public partial class MacDetay : System.Web.UI.UserControl
    {
        private int _macId;
        public int sehirId;
        public int sezonId;
        private int _ligId;
        private int _ilceId;

        private Mac m;
        private List<Mac> srcMac;
        DropDownList ddlIlce;
        DropDownList ddlStat;
        RadioButtonList rblDevre;
        DropDownList ddlLig;
        DropDownList ddlMacGrup;
        TextBox txtMacTarihi;
        TextBox txtMacSaati;
        DropDownList ddlHafta;
        TextBox txtMacKodu;
        DropDownList ddlEvSahibiTakim;
        DropDownList ddlMisafirTakim;
        DropDownList ddlMacSahaTipi;
        List<byte> haftaListesi;
        List<string> macSahaTipiListesi;

        private string _ilceAdi;
        private string _statAdi;
        private string _ligAdi;
        private string _macGrupAdi;
        private DateTime _macTarihi;
        private TimeSpan _macSaati;
        private byte _hafta;
        private string _macKodu;
        private string _evSahibiTakimAdi;
        private string _misafirTakimAdi;
        private string _macSahaTipi;

        private int _statId;
        private int _macGrupId;
        private int _evSahibiTakimId;
        private int _misafirTakimId;
        private int _devreId;

        protected void Page_Load(object sender, EventArgs e)
        {
            _macId = Convert.ToInt32(Request.QueryString["macId"]);
            _ilceId = MacOperation.IlceIdGetByMacId(_macId);
            _ligId = MacOperation.LigIdGetByMacId(_macId);

            GelenIdDegeriIleEntityNesnesiniGetir();
            DetailsViewDoldur();
            DetailsViewIcindekiKontrollerinIdleriniGetir();
        }

        private void GelenIdDegeriIleEntityNesnesiniGetir()
        {
            m = MacOperation.MacGetByMacId(_macId);
        }

        private void DetailsViewDoldur()
        {
            srcMac = new List<Mac>();
            srcMac.Add(m);
            DetailsView_MacDetay.DataSource = srcMac;
            DetailsViewBind();
        }

        private void DetailsViewIcindekiKontrollerinIdleriniGetir()
        {
            ddlIlce = (DropDownList)this.DetailsView_MacDetay.FindControl("ddlIlce");
            ddlStat = (DropDownList)this.DetailsView_MacDetay.FindControl("ddlStat");
            rblDevre = (RadioButtonList)this.DetailsView_MacDetay.FindControl("rblDevre");
            ddlLig = (DropDownList)this.DetailsView_MacDetay.FindControl("ddlLig");
            ddlMacGrup = (DropDownList)this.DetailsView_MacDetay.FindControl("ddlMacGrup");
            txtMacTarihi = (TextBox)this.DetailsView_MacDetay.FindControl("txtMacTarihi");
            txtMacSaati = (TextBox)this.DetailsView_MacDetay.FindControl("txtMacSaati");
            ddlHafta = (DropDownList)this.DetailsView_MacDetay.FindControl("ddlHafta");
            txtMacKodu = (TextBox)this.DetailsView_MacDetay.FindControl("txtMacKodu");
            ddlEvSahibiTakim = (DropDownList)this.DetailsView_MacDetay.FindControl("ddlEvSahibiTakim");
            ddlMisafirTakim = (DropDownList)this.DetailsView_MacDetay.FindControl("ddlMisafirTakim");
            ddlMacSahaTipi = (DropDownList)this.DetailsView_MacDetay.FindControl("ddlMacSahaTipi");
        }

        private void DetailsViewBind()
        {
            DetailsView_MacDetay.DataBind();
        }

        protected void DetailsView_MacDetay_ModeChanging(object sender, DetailsViewModeEventArgs e)
        {
            if (e.NewMode == DetailsViewMode.Edit)
            {
                DetailsView_MacDetay.ChangeMode(DetailsViewMode.Edit);
            }
            else if (e.NewMode == DetailsViewMode.Insert)
            {
                DetailsView_MacDetay.ChangeMode(DetailsViewMode.Insert);
            }
            else
            {
                DetailsView_MacDetay.ChangeMode(DetailsViewMode.ReadOnly);
            }

            DetailsViewBind();
        }

        protected void DetailsView_MacDetay_DataBound(object sender, EventArgs e)
        {
            if (this.DetailsView_MacDetay.CurrentMode == DetailsViewMode.Edit)
            {
                ddlIlce = (DropDownList)this.DetailsView_MacDetay.FindControl("ddlIlce");
                if (ddlIlce != null)
                {
                    ddlIlceDoldur();
                    ddlIlce.SelectedValue = MacOperation.IlceAdiGetByMacId(_macId);
                }

                ddlStat = (DropDownList)this.DetailsView_MacDetay.FindControl("ddlStat");
                if (ddlStat != null)
                {
                    ddlStatDoldur();
                    ddlStat.SelectedValue = MacOperation.StatAdiGetByMacId(_macId);
                }

                ddlLig = (DropDownList)this.DetailsView_MacDetay.FindControl("ddlLig");
                if (ddlLig != null)
                {
                    ddlLigDoldur();
                    ddlLig.SelectedValue = MacOperation.LigAdiGetByMacId(_macId);
                }

                ddlMacGrup = (DropDownList)this.DetailsView_MacDetay.FindControl("ddlMacGrup");
                if (ddlMacGrup != null)
                {
                    ddlMacGrupDoldur();
                    ddlMacGrup.SelectedValue = MacOperation.MacGrupAdiGetByMacId(_macId);
                }

                ddlHafta = (DropDownList)this.DetailsView_MacDetay.FindControl("ddlHafta");
                if (ddlHafta != null)
                {
                    ddlHaftaDoldur();
                    ddlHafta.SelectedValue = MacOperation.MacHaftaGetByMacId(_macId);
                }

                ddlEvSahibiTakim = (DropDownList)this.DetailsView_MacDetay.FindControl("ddlEvSahibiTakim");
                if (ddlEvSahibiTakim != null)
                {
                    ddlEvSahibiTakimDoldur();
                    ddlEvSahibiTakim.SelectedValue = MacOperation.EvSahibiTakimAdiGetByMacId(_macId);
                }

                ddlMisafirTakim = (DropDownList)this.DetailsView_MacDetay.FindControl("ddlMisafirTakim");
                if (ddlMisafirTakim != null)
                {
                    ddlMisafirTakimDoldur();
                    ddlMisafirTakim.SelectedValue = MacOperation.MisafirTakimAdiGetByMacId(_macId);
                }

                ddlMacSahaTipi = (DropDownList)this.DetailsView_MacDetay.FindControl("ddlMacSahaTipi");
                if (ddlMacSahaTipi != null)
                {
                    ddlMacSahaTipiDoldur();
                    ddlMacSahaTipi.SelectedValue = MacOperation.MacSahaTipiGetByMacId(_macId);
                }

                rblDevre = (RadioButtonList)this.DetailsView_MacDetay.FindControl("rblDevre");
                if (rblDevre != null)
                {
                    rblDevre.SelectedValue = MacOperation.DevreGetByMacId(_macId);
                }
            }
        }

        private void ddlMacSahaTipiDoldur()
        {
            macSahaTipiListesi = new List<string>();
            macSahaTipiListesi.Add("KALFA SUNİ ÇİM");

            ddlMacSahaTipi.DataSource = macSahaTipiListesi;
            ddlMacSahaTipi.DataBind();
        }

        private void ddlEvSahibiTakimDoldur()
        {
            ddlEvSahibiTakim.DataSource = TakimOperation.TakimAdiGetBySehirId(sehirId);
            ddlEvSahibiTakim.DataBind();
        }

        private void ddlMisafirTakimDoldur()
        {
            ddlMisafirTakim.DataSource = TakimOperation.TakimAdiGetBySehirId(sehirId);
            ddlMisafirTakim.DataBind();
        }

        private void ddlHaftaDoldur()
        {
            haftaListesi = new List<byte>();

            for (byte i = 1; i <= 40; i++)
            {
                haftaListesi.Add(i);
            }

            ddlHafta.DataSource = haftaListesi;
            ddlHafta.DataBind();
        }

        private void ddlMacGrupDoldur()
        {
            ddlMacGrup.DataSource = MacGrupOperation.MacGrupAdiGetAll();
            ddlMacGrup.DataBind();
        }

        private void ddlLigDoldur()
        {
            ddlLig.DataSource = LigOperation.LigGetBySehirId(sehirId);
            ddlLig.DataBind();
        }

        private void ddlStatDoldur()
        {
            ddlStat.DataSource = StatOperation.StatAdiGetBySehirId(sehirId);
            ddlStat.DataBind();
        }

        private void ddlIlceDoldur()
        {
            ddlIlce.DataSource = IlceOperation.IlceAdiGetBySehirId(Convert.ToString(sehirId));
            ddlIlce.DataBind();
        }

        protected void DetailsView_MacDetay_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            MacGuncelle();
            DuzenlemeModundanListelemeModunaGec();
        }

        private void MacGuncelle()
        {
            _ilceAdi = ddlIlce.SelectedValue;
            _ilceId = IlceOperation.IlceIdGetByIlceAdiAndSehirId(_ilceAdi, Convert.ToString(sehirId));
            _statAdi = ddlStat.SelectedValue;
            _statId = StatOperation.StatIdGetByStatAdi(_statAdi);
            _devreId = Convert.ToInt32(rblDevre.SelectedValue);
            _ligAdi = ddlLig.SelectedValue;
            _ligId = LigOperation.LigIdGetByLigAdi(_ligAdi);
            _macGrupAdi = ddlMacGrup.SelectedValue;
            _macGrupId = MacGrupOperation.MacGrupIdGetByMacGrupAdi(_macGrupAdi);
            _macTarihi = Convert.ToDateTime(txtMacTarihi.Text.Trim());
            _macSaati = TimeSpan.Parse(txtMacSaati.Text);
            _hafta = Byte.Parse(ddlHafta.SelectedValue);
            _macKodu = txtMacKodu.Text.Trim();
            _evSahibiTakimAdi = ddlEvSahibiTakim.SelectedValue;
            _evSahibiTakimId = TakimOperation.TakimIdGetByTakimAdi(_evSahibiTakimAdi);
            _misafirTakimAdi = ddlMisafirTakim.SelectedValue;
            _misafirTakimId = TakimOperation.TakimIdGetByTakimAdi(_misafirTakimAdi);
            _macSahaTipi = ddlMacSahaTipi.SelectedValue;

            m.IlceID = _ilceId;
            m.StatID = _statId;
            m.DevreID = _devreId;
            m.LigID = _ligId;
            m.MacGrupID = _macGrupId;
            m.MacTarihi = _macTarihi;
            m.MacSaati = _macSaati;
            m.MacHafta = _hafta;
            m.MacKodu = _macKodu;
            m.EvSahibiTakimID = _evSahibiTakimId;
            m.MisafirTakimID = _misafirTakimId;
            m.MacSahaTipi = _macSahaTipi;
            m.DegisiklikTarihi = DateTime.Now;

            MacOperation.MacUpdate(m);
        }

        private void DuzenlemeModundanListelemeModunaGec()
        {
            DetailsView_MacDetay.ChangeMode(DetailsViewMode.ReadOnly);
            DetailsViewBind();
        }
    }
}